package com.haizhi.crm.sys.message.service.impl;

import com.haizhi.crm.common.log.GLog;
import com.haizhi.crm.common.log.LogFactory;
import com.haizhi.crm.sys.message.dao.MessageCategoryDao;
import com.haizhi.crm.sys.message.model.MessageCategoryDo;
import com.haizhi.crm.sys.message.model.MessageCategoryVo;
import com.haizhi.crm.sys.message.service.MessageCategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by wangshan on 2018/5/16.
 */
@Service
public class MessageCategoryServiceImpl implements MessageCategoryService {

    private static final GLog LOG = LogFactory.getLogger(MessageCategoryServiceImpl.class);
    public static final Long ROOT_NODE_PARENT_ID = -1L;
    public static final int ROOT_NODE_LEVEL = 1;

    @Autowired
    private MessageCategoryDao messageCategoryDao;

    @Override
    public List<MessageCategoryVo> query() {
        return query(ROOT_NODE_PARENT_ID);
    }

    /**
     * recursive query message category by parentId
     *
     * @param parentId
     * @return
     */
    private List<MessageCategoryVo> query(Long parentId) {
        if (parentId == null) {
            parentId = ROOT_NODE_PARENT_ID;
        }
        List<MessageCategoryVo> messageCategoryVos = new ArrayList<>();
        List<MessageCategoryDo> messageCategoryDos = messageCategoryDao.findByParentId(parentId);
        for (MessageCategoryDo messageCategoryDo : messageCategoryDos) {
            MessageCategoryVo messageCategoryVo = new MessageCategoryVo(messageCategoryDo);
            Long id = messageCategoryDo.getId();
            if (!messageCategoryDo.isLeaf()) {
                //recursive query
                messageCategoryVo.setMessageCategoryVoList(query(id));
            }
            messageCategoryVos.add(messageCategoryVo);
        }
        if (messageCategoryVos == null || messageCategoryVos.size() == 0) {
            return null;
        }
        return messageCategoryVos;
    }

    @Override
    public void saveOrUpdate(List<MessageCategoryVo> messageCategoryVos) {
        save(messageCategoryVos, null, 0);
    }

    /**
     * recursive save message category
     *
     * @param messageCategoryVos
     * @param parentId
     * @param level
     */
    private void save(List<MessageCategoryVo> messageCategoryVos, Long parentId, int level) {
        if (parentId == null) {
            parentId = ROOT_NODE_PARENT_ID;
            level = ROOT_NODE_LEVEL;
        }
        for (MessageCategoryVo messageCategoryVo : messageCategoryVos) {
            MessageCategoryDo messageCategoryDo = new MessageCategoryDo();
            String description = messageCategoryVo.getDescription();
            String name = messageCategoryVo.getName();
            messageCategoryDo.setName(name);
            messageCategoryDo.setDescription(description);
            messageCategoryDo.setParentId(parentId);
            messageCategoryDo.setLevel(level);
            List<MessageCategoryVo> messageCategoryVos1 = messageCategoryVo.getMessageCategoryVoList();
            if (messageCategoryVos1 == null || messageCategoryVos1.size() == 0) {
                messageCategoryDo.setLeaf(true);
            } else {
                messageCategoryDo.setLeaf(false);
            }
            MessageCategoryDo messageCategoryDo1 = messageCategoryDao.save(messageCategoryDo);
            Long parentId1 = messageCategoryDo1.getId();
            if (messageCategoryVos1 == null || messageCategoryVos1.size() == 0) {
                ;
            } else {
                //recursive save
                save(messageCategoryVos1, parentId1, level + 1);
            }
        }
    }

    @Override
    public void delete(String name) {
        messageCategoryDao.deleteByName(name);
    }

    @Override
    public void delete(Long msgCategoryId) {
        messageCategoryDao.deleteById(msgCategoryId);
    }

    @Override
    public void deleteAll() {
        messageCategoryDao.deleteAll();
    }


    @Override
    public MessageCategoryDo find(String sourceName) {
        return messageCategoryDao.findBySourceName(sourceName);
    }

    /**
     * get message category parent type by child type id
     *
     * @param messageCategoryId
     * @return
     */
    @Override
    public MessageCategoryDo findParentTypeById(Long messageCategoryId) {
        MessageCategoryDo messageCategoryDo = messageCategoryDao.findById(messageCategoryId);
        if (messageCategoryDo.getParentId() == ROOT_NODE_PARENT_ID) {
            return messageCategoryDo;
        }
        return findParentTypeById(messageCategoryDo.getParentId());
    }
}
